package com.qztc.news_end_customer.controller;

import com.qztc.news_end_customer.config.GlobalConfig;
import com.qztc.news_end_dto.dto.Comment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/thenews/comment")
public class CommentControllerByRestTemplate extends GlobalConfig {
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/add")
    public String addComment(@ModelAttribute Comment comment) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Comment> httpEntity = new HttpEntity<>(comment,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"comment/add", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }

    @GetMapping("/getAll")
    public String getAllComment(@RequestParam Integer newsid) {
        return restTemplate.getForObject(SEVICE_URL+"comment/getAll?newsid="+newsid, String.class);
    }

    @DeleteMapping("/deletebycommentid")
    public String deleteComment(@RequestBody Comment comment) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Comment> httpEntity = new HttpEntity<>(comment,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"comment/deletebycommentid", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @GetMapping("/get")
    public String getComment() {
        return restTemplate.getForObject(SEVICE_URL+"comment/get", String.class);
    }
}
